package com.simu.towercrane.wxapi;

import com.simu.towercrane.R;
import com.simu.towercrane.wxutils.SystemUtil;
import com.simu.towercrane.wxutils.WXUtil;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
	private final String WX_PACKAGE_NAME = "com.simu.towercrane";

	// APP_ID 替换为你的应用从官方网站申请到的合法appId
//    public static final String APP_ID = "wxd930ea5d5a258f4f";
    public static final String APP_ID = "wx382ef345a728697e";

    private Button wxBtn;
	private Button sendTextBtn;
//	private Button sendPicBtn;
//	private Button sendLocalBtn;
	private Button sendUrlBtn;
//	private Button cameraBtn;
	private String urlImg;
//	private Button musicBtn;
	private ImageView iv = null;

	// IWXAPI 是第三方app和微信通信的openapi接口
	private IWXAPI api;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent=getIntent();
		urlImg=intent.getStringExtra("url");
		// 通过WXAPIFactory工厂，获取IWXAPI的实例
		api = WXAPIFactory.createWXAPI(WXEntryActivity.this, APP_ID, true);
		api.handleIntent(getIntent(), WXEntryActivity.this);
		api.registerApp(APP_ID);

		initView();

		setFunction();
	}

	private void initView() {

		LinearLayout layout = new LinearLayout(this);
		
		layout.setOrientation(LinearLayout.VERTICAL);
		
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		
		layout.setLayoutParams(params);

		LinearLayout.LayoutParams btnParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

		wxBtn = new Button(this);
		wxBtn.setText("是否安装微信");
		wxBtn.setLayoutParams(btnParams);

		sendTextBtn = new Button(this);
		sendTextBtn.setText("发送文字");
		sendTextBtn.setLayoutParams(btnParams);

//		sendPicBtn = new Button(this);
//		sendPicBtn.setText("上传照片");
//		sendPicBtn.setLayoutParams(btnParams);
//
//		sendLocalBtn = new Button(this);
//		sendLocalBtn.setText("上传本地照片");
//		sendLocalBtn.setLayoutParams(btnParams);

		sendUrlBtn = new Button(this);
		sendUrlBtn.setText("上传Url照片");
		sendUrlBtn.setLayoutParams(btnParams);

//		cameraBtn = new Button(this);
//		cameraBtn.setText("照相上传");
//		cameraBtn.setLayoutParams(btnParams);
//
//		musicBtn = new Button(this);
//		musicBtn.setText("上传Music");
//		musicBtn.setLayoutParams(btnParams);

		iv = new ImageView(this);

		layout.addView(wxBtn);
		layout.addView(sendTextBtn);
//		layout.addView(sendPicBtn);
//		layout.addView(sendLocalBtn);
		layout.addView(sendUrlBtn);
//		layout.addView(cameraBtn);
//		layout.addView(musicBtn);

		layout.addView(iv);
		setContentView(layout);

		sendTextBtn.setVisibility(View.GONE);
//		sendPicBtn.setVisibility(View.GONE);
//		sendLocalBtn.setVisibility(View.GONE);
		sendUrlBtn.setVisibility(View.GONE);
//		cameraBtn.setVisibility(View.GONE);
//		musicBtn.setVisibility(View.GONE);

	}
	private void IsHavedWx(){
		boolean result = new SystemUtil(WXEntryActivity.this).isInstallWx(WX_PACKAGE_NAME);

		if (result) {

			Toast.makeText(WXEntryActivity.this, "已安装微信",Toast.LENGTH_SHORT).show();
			wxBtn.setVisibility(View.GONE);
			sendTextBtn.setVisibility(View.VISIBLE);
//			sendPicBtn.setVisibility(View.VISIBLE);
//			sendLocalBtn.setVisibility(View.VISIBLE);
			sendUrlBtn.setVisibility(View.VISIBLE);
//			cameraBtn.setVisibility(View.VISIBLE);
//			musicBtn.setVisibility(View.VISIBLE);

		} else {
			Toast.makeText(WXEntryActivity.this, "未安装微信",Toast.LENGTH_SHORT).show();
		}
	}
	private void setFunction() {
		IsHavedWx();
//		wxBtn.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View v) {
//
//				boolean result = new SystemUtil(WXEntryActivity.this).isInstallWx(WX_PACKAGE_NAME);
//
//				if (result) {
//
//					Toast.makeText(WXEntryActivity.this, "已安装微信",Toast.LENGTH_SHORT).show();
//					wxBtn.setVisibility(View.GONE);
//					sendTextBtn.setVisibility(View.VISIBLE);
////					sendPicBtn.setVisibility(View.VISIBLE);
////					sendLocalBtn.setVisibility(View.VISIBLE);
//					sendUrlBtn.setVisibility(View.VISIBLE);
////					cameraBtn.setVisibility(View.VISIBLE);
////					musicBtn.setVisibility(View.VISIBLE);
//
//				} else {
//					Toast.makeText(WXEntryActivity.this, "未安装微信",Toast.LENGTH_SHORT).show();
//				}
//			}
//		});

		sendTextBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				
				new WXUtil(WXEntryActivity.this, api).updateWXStatus("大家好","测试");

				finish();
			}
		});

//		sendPicBtn.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
//
//				new WXUtil(WXEntryActivity.this, api).uploadPic(bmp);
//
//				finish();
//
//			}
//		});

//		sendLocalBtn.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				
//				new SystemUtil(WXEntryActivity.this).openLocalPic();
//			}
//		});

		sendUrlBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				new Thread() {
					public void run() {
						new WXUtil(WXEntryActivity.this, api).uploadUrlPic(urlImg);

						finish();
					}
				}.start();
			}
		});

//		cameraBtn.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				
//				new SystemUtil(WXEntryActivity.this).openCamera();
//
//			}
//		});

//		musicBtn.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				new WXUtil(WXEntryActivity.this, api).uploadMusic("http://staff2.ustc.edu.cn/~wdw/softdown/index.asp/0042515_05.ANDY.mp3");
//
//				finish();
//			}
//		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == 0) {
			if (resultCode == RESULT_OK) {

				new WXUtil(WXEntryActivity.this, api).uploadLocalPic(data);

				finish();
			}
		}
		if (requestCode == 1) {
			if (resultCode == RESULT_OK) {

				new WXUtil(WXEntryActivity.this, api).uploadCameraPic(data);

				finish();
			}
		}
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);

		setIntent(intent);
		api.handleIntent(intent, this);
	}

	// 微信发送请求到第三方应用时，会回调到该方法
	@Override
	public void onReq(BaseReq req) {
		// TODO
	}

	// 第三方应用发送到微信的请求处理后的响应结果，会回调到该方法
	@Override
	public void onResp(BaseResp resp) {

		String result = "";
		switch (resp.errCode) {
		case BaseResp.ErrCode.ERR_OK:
			result = "发送成功";
			break;
		case BaseResp.ErrCode.ERR_USER_CANCEL:
			result = "取消";
			break;
		case BaseResp.ErrCode.ERR_AUTH_DENIED:
			result = "发送失败";
			break;
		default:
			result = "出现异常";
			break;
		}
		Toast.makeText(this, result, Toast.LENGTH_LONG).show();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			AlertDialog.Builder dialog = new AlertDialog.Builder(
					WXEntryActivity.this);
			dialog.setTitle("退出");
			dialog.setMessage("点击确定退出编辑");
			dialog.setPositiveButton("确定",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							finish();
						}
					});
			dialog.setNegativeButton("取消", null).show();
		}
		return false;
	}
}
